﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alipay.AopSdk.AspnetCore;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core.Response;

namespace XiaoMiAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MoneyController : ControllerBase
    {
        //通过di注入
        private readonly AlipayService _alipayService;
        public MoneyController(AlipayService alipayService)
        {
            _alipayService = alipayService;
        }
        [HttpGet]
        public IActionResult AliPayShow()
        {
            // 外部订单号，商户网站订单系统中唯一的订单号
            string out_trade_no = System.Guid.NewGuid().ToString();

            // 订单名称
            string subject = "测试商品";

            // 付款金额
            string total_amount = "0.1";

            // 商品描述
            string body = "无描述";

            // 组装业务参数model
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = total_amount;
            model.OutTradeNo = out_trade_no;
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";

            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
            request.SetReturnUrl("");
            // 设置异步通知接收地址
            request.SetNotifyUrl("");
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradePagePayResponse response = null;
            try
            {

                response = _alipayService.PageExecute(request, null, "post");
                //Response.Write(response.Body);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return Ok(response.Body);
        }
    
}
}
